<?php

namespace app\api\controller\order;


use addons\epay\library\Service;
use app\admin\model\Conference;
use app\admin\model\trades\assets\Order;
use app\common\controller\Api;
use app\common\library\Iden;
use Exception;
use think\Db;
use app\admin\model\Myservice;
use app\common\controller\Backend;
use think\exception\PDOException;
use think\exception\ValidateException;
use app\admin\model\trades\order\Gainlog;
use app\admin\model\trades\order\Gain;

// 允许跨域
header('Access-Control-Allow-Origin:*');//允许跨域

/**
 * 个人中心
 *加入活动
 */
class Payment extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $model = null;
    protected $catemodel = null;
    protected $tagsmodel = null;
    protected $actmodel = null;
    protected $ticket_model = null;
    protected $aact = null;
    protected $api_act = null;
    protected $mem_model = null;
    protected $Infomodel = null;
    protected $gainlogmodel = null;
    protected $gainmodel = null;

    public function _initialize()
    {

        parent::_initialize();
        $this->model = new \app\admin\model\trades\assets\Order;//订单
        $this->Infomodel = new \app\admin\model\trades\assets\Orderinfo;//订单详情
        $this->gainlogmodel = new \app\admin\model\trades\order\Gainlog;//收益详情
        $this->gainmodel = new \app\admin\model\trades\order\Gain;//收益总表
        $this->actmodel = new \app\admin\model\vactivities\Activity;
        $this->catemodel = new \app\admin\model\superdata\Activitycategory;
        $this->tagsmodel = new \app\admin\model\superdata\Activitytags;
        $this->ticket_model = new \app\admin\model\vactivities\Ticket;
        $this->admin_model = new \app\admin\model\Admin;
        $this->mem_model = new \app\admin\model\member\Account;

    }


    /**
     * 支付成功，回调函数
     */
    public function notifyx()
    {
        $paytype = $this->request->param('paytype','wechat');
        $pay = Service::checkNotify($paytype);

        if (!$pay) {
            $data = '签名错误';
            return  $data;
        }

        $data = $pay->verify();
        Db::startTrans();
        try {
            $orderid = $data['out_trade_no'];
            $sn =$data['transaction_id'];

            $row = $this->model->get(['orderid' => $orderid]);
            if (!$row) {
                $this->error(__('No Results were found'));
            }
            $order = $row->toArray();//订单信息

            $amount = $order['amount'];
            $oid = $order['id'];
            $admin_id = $order['account_id'];
            if($data['result_code']==='SUCCESS' && $data['return_code']==='SUCCESS'){
                Order::update_order($orderid,$sn);//订单表更新
                Gainlog::update_gain_log($oid);//收益详情表更新 $oid是自动生成的订单id
                Gain::update_gain($admin_id,$amount);//收益总表更新
                Db::commit();
            }
            //你可以在此编写订单逻辑
        } catch (ValidateException $e) {
             Db::rollback();
            $this->error($e->getMessage());
        } catch (PDOException $e) {
             Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        return $pay->success();
    }


    public function returnx()
    {
        $paytype = $this->request->param('paytype','wechat');
        $out_trade_no = $this->request->param('out_trade_no');
//        $pay = Service::checkReturn($paytype);
//        if (!$pay) {
//            $this->error('签名错误', '');
//        }

        //你可以在这里通过out_trade_no去验证订单状态
        //但是不可以在此编写订单逻辑！！！
        $r =  Db::name("activity_order")->where('orderid',$out_trade_no)->find();
        if($r['state']!=2){
            $this->error('支付状态失败');
        }
        $this->success("支付成功", $r,200);
    }
}
